﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AteneoLibriBusinessLogic.Entities;
using AteneoLibriBusinessLogic.MyException;

namespace AteneoLibriBusinessLogic.DAL
{
    public class UniversityFacade
    {

        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        public List<University> getUniversities()
        {
            List<University> list = new List<University>();
            try
            {
                IEnumerable<AteneoLibriEntityFramework.University> universities = new List<AteneoLibriEntityFramework.University>();
                universities = (from u in db.Universities select u);
               
                foreach (var item in universities)
                {
                    University uni = TOA.UniversityAssembler.Assembler(item);
                    list.Add(uni);
                }
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("ERROR: [METHOD: getUniversities()] " + ex.Message);
            }

            return list;
        }

        public University getUniversity(Guid idUniversity)
        {
            University university = null;
            try
            {
                AteneoLibriEntityFramework.University universityEF = db.Universities.Where(x => x.IdUniversity == idUniversity).FirstOrDefault();
                university = TOA.UniversityAssembler.Assembler(universityEF);
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }
            return university;
        }

        public List<University> getUniversityByCityId(Guid cityId)
        {
            List<University> universities = getUniversities().Where(x => x.CityId == cityId).ToList();
            return universities;
        }

        public bool insertUniversity(University university)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.University universityEF = TOA.UniversityAssembler.DeAssembler(university);
                db.Universities.AddObject(universityEF);
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }
            return result;
        }

        public bool updateUniversity(University university)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.University universityEF = db.Universities.Where(x => x.IdUniversity == university.IdUniversity).FirstOrDefault();
                TOA.UniversityAssembler.WriteToEntity(universityEF, university);

                db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool deleteUniversity(University university)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.University uniEF = db.Universities.Where(x => x.IdUniversity == university.IdUniversity).FirstOrDefault();
                db.DeleteObject(uniEF);
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }
    }
}
